import os


def get_file_list(path, filetype=".csv"):
    """
    + 输入路径
    + :return 文件夹路径+文件名
    + get_file_list(paths)
    """
    file_list_all = []
    ret = os.listdir(path)
    for file in ret:
        # print("-" * 20 + "found proxy file %s" % file + "-"*20)
        if file.endswith(filetype):
            file_list = os.path.join(path, file)
            file_list_all.append(file_list)
    return file_list_all


def file_chack(pathList):
    """
    + 检查是否有无效文件
    pathss = 路径
    """
    for paths in pathList:
        if os.path.isfile(paths) and os.path.exists(paths):
            with open(paths, "r") as f:
                con = f.read()
            if con == "":
                os.remove(paths)
                print(paths, "removed")
            else:
                # print(paths, "good files")
                pass
    print("file chack done")


def DirPathChick(new_paths):
    """
    检查文件夹是否存在，不存在新建
    """
    if os.path.isdir(new_paths) and os.path.exists(new_paths):
        pass
    else:
        os.makedirs(new_paths)


def FileCheck(new_paths):
    """
    检查文件是否存在
    """
    if os.path.isfile(new_paths) and os.path.exists(new_paths):
        # print(True,"file ex")
        return True
    return False


def AutoRmove(path, filename):
    """
    删除历史文件
    """
    dirl = os.listdir(path)
    st = filename
    st = st.split("_")
    if len(st) > 1:
        st = st[-1]
        for dirs in dirl:
            if dirs.endswith(st):
                pass
            else:
                di = path + dirs
                print("{}==>  removed {} not meching".format(di, st))
                os.remove(di)
    else:
        print("not matching")